package com.xuecheng.system.controller;

import com.xuecheng.commons.enums.ErrorCode;
import com.xuecheng.commons.model.vo.AuthInfo;
import com.xuecheng.commons.model.vo.CompanyVo;
import com.xuecheng.commons.model.vo.ResponseResult;
import com.xuecheng.commons.utils.AuthInfoHolder;
import com.xuecheng.commons.utils.BeanHelper;
import com.xuecheng.commons.utils.JwtUtils;
import com.xuecheng.system.domain.Company;
import com.xuecheng.web.exception.BusinessException;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RestController;
import com.xuecheng.system.service.CompanyService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;

/**
 * <p>
 * 教育机构前端控制器
 * </p>
 *
 * @author itheima
 */
@Slf4j
@RestController
public class CompanyController {

    @Autowired
    private CompanyService  companyService;


    // 机构查询
/*    @GetMapping("/company/mine")
    public ResponseResult findCompany(@RequestHeader("Authorization") String token){// 1.接收token
        // 2.校验token合法性
        Boolean verifyToken = JwtUtils.verifyToken(token);
        if(!verifyToken){
            throw new BusinessException(ErrorCode.NOTTOKEN);
        }
        // 3.解析token获取authInfo对象
        AuthInfo authInfo = JwtUtils.getInfoFromToken(token);
        // 4.调用service查询企业对象
        System.out.println(">>>>>>>>>>>>>>>>>>>"+authInfo.getCompanyId());
        Company company = companyService.getById(authInfo.getCompanyId());
        // 5.实体转vo
        CompanyVo companyVo = BeanHelper.copyProperties(company, CompanyVo.class);
        // 6.返回结果
        return ResponseResult.okResult(companyVo);

    }*/

    //网关改进
    @GetMapping("/company/mine")
    public ResponseResult findCompany(){
        // 1.通过threadLocal获取authInfo
        AuthInfo authInfo = AuthInfoHolder.getAuthInfo();
        // 2.调用service查询企业对象
        Company company = companyService.getById(authInfo.getCompanyId());
        // 3.实体转vo
        CompanyVo companyVo = BeanHelper.copyProperties(company, CompanyVo.class);
        // 4.返回结果
        return ResponseResult.okResult(companyVo);

    }
}